package com.txt.call;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.library.control.TxtKandy;
import com.example.library.login.AccessKandy;

/**
 * Created by DELL on 2017/8/17.
 */

public class PhoneRegisterActivity extends Activity {
    private EditText mPhoneEditText;
    private EditText mMsgCode;
    private Button  mSendButton;
    private Button mLoginButton;
    private int time=60;
    private final String TAG=PhoneRegisterActivity.class.getSimpleName();
    private ProgressDialog mProgreeDialog;
    public static final String LOGIN="login";
    private Handler mTimeHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==0){
                if (time>0){
                    mSendButton.setText(time+"");
                    time--;
                    mTimeHandler.sendEmptyMessageDelayed(0,1000);
                }else {
                    time=60;
                    mSendButton.setText("发送验证码");
                }
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_register);
        initView();
    }
    private void initView() {
        mPhoneEditText= (EditText) findViewById(R.id.phone_edittext);
        mMsgCode= (EditText) findViewById(R.id.code_edit);
        mSendButton= (Button) findViewById(R.id.sendmsgcode);
        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPhoneEditText.getText().equals("")){
                    Toast.makeText(PhoneRegisterActivity.this,"手机号不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                TxtKandy.getAccessKandy().sendVerificationCode(mPhoneEditText.getText().toString(), new AccessKandy.RequestCallBack() {
                    @Override
                    public void onSuccess() {
                        PhoneRegisterActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(PhoneRegisterActivity.this,"发送成功",Toast.LENGTH_SHORT).show();
                            }
                        });
                        mTimeHandler.sendEmptyMessage(0);
                    }
                    @Override
                    public void onFail(int errCode, String errMsg) {
                        Toast.makeText(PhoneRegisterActivity.this,"发送失败"+errMsg,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        mLoginButton= (Button) findViewById(R.id.phone_login);
        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPhoneEditText.getText().equals("")){
                    Toast.makeText(PhoneRegisterActivity.this,"手机号不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mMsgCode.getText().equals("")){
                    Toast.makeText(PhoneRegisterActivity.this,"验证码内容不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                mProgreeDialog= ProgressDialog.show(PhoneRegisterActivity.this,null,"正在登录用户，请稍后....",true,false);
                mProgreeDialog.setCancelable(true);
                TxtKandy.getAccessKandy().validate(mPhoneEditText.getText().toString(), mMsgCode.getText().toString(), new AccessKandy.RequestCallBack() {
                    @Override
                    public void onSuccess() {
                        userLogin();
                    }
                    @Override
                    public void onFail(int i, final String s) {
                        PhoneRegisterActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mProgreeDialog.dismiss();
                                Toast.makeText(PhoneRegisterActivity.this,s,Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }
        });
    }
    public void userLogin(){
        TxtKandy.getAccessKandy().userLogin(TxtKandy.getAccessKandy().mUser, TxtKandy.getAccessKandy().mPassward, new AccessKandy.LoginRequestCallBack() {
            @Override
            public void onSuccess() {
               PhoneRegisterActivity.this.runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       mProgreeDialog.dismiss();
                       Intent intent=new Intent();
                       intent.putExtra(LOGIN,true);
                       PhoneRegisterActivity.this.setResult(RESULT_OK,intent);
                       PhoneRegisterActivity.this.finish();
                   }
               });
            }
            @Override
            public void onFail(int i,final String s) {
                Log.d(TAG, "onFail: loginFail"+i+"ErrMsg"+s);
                PhoneRegisterActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mProgreeDialog.dismiss();
                        Toast.makeText(PhoneRegisterActivity.this,s,Toast.LENGTH_SHORT).show();
                    }
                });

            }
        });
    }
}
